﻿Imports System.Reflection
Imports System.Text

Public Class XscTypeAdd
    Public Sub New()

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。

    End Sub

    Public Sub New(ByVal typeId As Integer)

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        typeId = typeId
    End Sub

    Private _typeId As Integer = 0
    Public Property TypeId() As Integer
        Get
            Return _typeId
        End Get
        Set(ByVal value As Integer)
            _typeId = value
        End Set
    End Property

    Public Overrides Sub InitAdd()
        txtParentName.Tag = ""
        If _typeId = 0 Then
            TypeId = 1
        End If
    End Sub

    Dim _TextBrowser As String = ""
    Public Property TextBrowser() As String
        Get
            Return _TextBrowser
        End Get
        Set(ByVal value As String)
            _TextBrowser = value
        End Set
    End Property
    Public Overrides Function DoAddCheck() As Boolean
        MyBase.DoAddCheck()
        If String.IsNullOrEmpty(Me.MyEdit1.Text.TrimEnd()) Then
            ShowMessageBox("名称不能为空")
            Return False
        End If
        Return True
    End Function

    Public Overrides Function DoInsert() As Boolean
        MyBase.DoInsert()
        Try
            Dim strSql As String = ""

            'D-2010-03-10 避免数据库错误
            'If (String.IsNullOrEmpty(CStr(Me.txtParentName.Tag).TrimEnd())) Then
            '    strSql = "insert into  XscBugType(type,name,ParentID,USERNAME,OPTIME,AddTime,OptUser) values('" + Me.TypeId.ToString() + "','" + Me.MyEdit1.Text.TrimEnd() + "',NULL,'" + strName + "','" + DateTime.Now.ToString() + "','" + DateTime.Now.ToString() + "','" + strName + "')"
            'Else
            '    strSql = "insert into  XscBugType(type,name,ParentID,USERNAME,OPTIME,AddTime,OptUser) values('" + Me.TypeId.ToString() + "','" + Me.MyEdit1.Text.TrimEnd() + "','" + CStr(Me.txtParentName.Tag).TrimEnd() + "','" + strName + "','" + DateTime.Now.ToString() + "','" + DateTime.Now.ToString() + "','" + strName + "')"
            'End If
            'D-2010-03-10 避免数据库错误

            'A-2010-03-10 避免数据库错误
            Dim cmd As New ADODB.Command
            If (String.IsNullOrEmpty(CStr(Me.txtParentName.Tag).TrimEnd())) Then
                strSql = "insert into  XscBugType(type,name,Memo,ParentID,USERNAME,OPTIME,AddTime,OptUser) values(?,?,?,NULL,?,?,?,?)"
            Else
                strSql = "insert into  XscBugType(type,name,Memo,ParentID,USERNAME,OPTIME,AddTime,OptUser) values(?,?,?,?,?,?,?,?)"
            End If
            cmd.CommandText = strSql
            '类型编号
            Dim pa As ADODB.Parameter = cmd.CreateParameter("type", ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput, 10, Me.TypeId.ToString())
            cmd.Parameters.Append(pa)

            '名称
            pa = cmd.CreateParameter("name", ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput, 150, Me.MyEdit1.Text.TrimEnd())
            cmd.Parameters.Append(pa)

            '备注
            pa = cmd.CreateParameter("Memo", ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput, 50, Me.txtMemo.Text.TrimEnd())
            cmd.Parameters.Append(pa)

            '父
            If (Not String.IsNullOrEmpty(CStr(Me.txtParentName.Tag).TrimEnd())) Then
                pa = cmd.CreateParameter("ParentID", ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput, 10, CStr(Me.txtParentName.Tag).TrimEnd())
                cmd.Parameters.Append(pa)
            End If

            '用户名称
            pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput, 50)
            pa.Value = strName
            cmd.Parameters.Append(pa)

            '操作时间
            pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adDBTimeStamp, ADODB.ParameterDirectionEnum.adParamInput, 100)
            pa.Value = DateTime.Now.ToString()
            cmd.Parameters.Append(pa)

            '操作时间
            pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adDBTimeStamp, ADODB.ParameterDirectionEnum.adParamInput, 100)
            pa.Value = DateTime.Now.ToString()
            cmd.Parameters.Append(pa)

            '用户名称
            pa = cmd.CreateParameter(, ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput, 50)
            pa.Value = strName
            cmd.Parameters.Append(pa)

            If (ct.ExcCommand(cmd)) Then
                bAdd = True
                ShowMessageBox("插入成功")
                txtParentName.Text = ""
                Me.MyEdit1.Text = ""
                Me.txtParentName.Tag = ""
                Return True
            End If
            ''A-2010-03-10 避免数据库错误

            'D-2010-03-10 避免数据库错误
            'ct.GetUpdatePageRs(strSql)
            'D-2010-03-10 避免数据库错误
            Return False
        Catch ex As Exception
            ShowMessageBox("插入失败")
            UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, ex, MethodBase.GetCurrentMethod.GetType())
            Return False
        End Try
        Return True
    End Function

    Public Overrides Function DoAddExist() As Boolean
        MyBase.DoAddExist()
        Try
            Dim strSql As String = "select * from XscBugType where type='" + Me.TypeId.ToString() + "' And name='" + Me.MyEdit1.Text.TrimEnd() + "'"
            If UTIL.CUtil.ConvertText(Me.txtParentName.Tag) = "" Then
                strSql &= " And ParentID IS Null"
            Else
                strSql &= " And ParentID='" + UTIL.CUtil.ConvertText(Me.txtParentName.Tag) + "'"
            End If
            If ct.GetExistSql(strSql) Then
                ShowMessageBox("该名称数据已经存在")
                Return False
            End If
            Return True
        Catch ex As Exception
            ShowMessageBox("插入失败")
            UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, ex, MethodBase.GetCurrentMethod.GetType())
            Return False
        End Try
        Return True
    End Function

    Private Sub MyButton1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyButton1.Click

        Dim ttf As Wys.Control.Vb.TreeTableFromBase = New Wys.Control.Vb.TreeTableFromBase(Me.txtParentName.Tag.ToString())
        ttf.ID = "ID"
        ttf.ParentID = "ParentID"
        ttf.DisplayName = "Name"
        ttf.TableName = "XscBugType"
        ttf.Text = TextBrowser
        ttf.TypeId = TypeId
        ttf.ShowDialog()
        If (Not ttf.Cancel) Then
            txtParentName.Text = ttf.DisplayNameValue
            txtParentName.Tag = ttf.Result
        End If
        txtParentName.Text = ct.GetParentName(txtParentName.Tag.ToString(), "XscBugType", "Name", "ParentID", "ID")
    End Sub

#Region "更新模式"

    Public Overrides Function DoUpdate() As Boolean
        MyBase.DoUpdate()
        DoUpdate = False
        Dim sb As New StringBuilder

        sb.Append(" Update XscBugType set Name=?,Memo=?,ParentID=?,OPTIME=?,OptUser=? where ID=? And Type=? ")
        Dim cmd As New ADODB.Command

        Dim pa As ADODB.Parameter = cmd.CreateParameter("Name", ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput, 50, Me.MyEdit1.Text.TrimEnd)
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter("Memo", ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput, 50, Me.txtMemo.Text.TrimEnd)
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter("ParentID", ADODB.DataTypeEnum.adVarNumeric, ADODB.ParameterDirectionEnum.adParamInput)
        pa.NumericScale = 10
        pa.Precision = 0
        If Me.txtParentName.Tag = "" Then
            pa.Value = DBNull.Value
        Else
            pa.Value = Me.txtParentName.Tag
        End If
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter("OPTIME", ADODB.DataTypeEnum.adDBTimeStamp, ADODB.ParameterDirectionEnum.adParamInput, 100, DateTime.Now.ToString())
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter("OptUser", ADODB.DataTypeEnum.adVarWChar, ADODB.ParameterDirectionEnum.adParamInput, strName.Length, strName)
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter("ID", ADODB.DataTypeEnum.adVarNumeric, ADODB.ParameterDirectionEnum.adParamInput)
        pa.NumericScale = 10
        pa.Precision = 0
        If Me.MyEdit1.Tag = "" Then
            pa.Value = DBNull.Value
        Else
            pa.Value = Me.MyEdit1.Tag
        End If
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter("Type", ADODB.DataTypeEnum.adVarNumeric, ADODB.ParameterDirectionEnum.adParamInput)
        pa.NumericScale = 10
        pa.Precision = 0
        pa.Value = TypeId
        cmd.Parameters.Append(pa)

        cmd.CommandText = sb.ToString()

        If ct.ExcCommand(cmd) Then
            DoUpdate = True
            ShowMessageBox("更新成功")
            bAdd = True
            Me.Close()
        Else
            DoUpdate = False
            ShowMessageBox("更新失败")
        End If
        DoUpdate = True
    End Function

    Public Overrides Function DoUpdateCheck() As Boolean
        MyBase.DoUpdateCheck()
        DoUpdateCheck = False
        If UTIL.CUtil.ConvertText(Me.MyEdit1.Tag) = "" Then
            ShowMessageBox("请选择要更新的记录")
            Return False
        End If

        If String.IsNullOrEmpty(Me.MyEdit1.Text.TrimEnd()) Then
            ShowMessageBox("名称不能为空")
            Return False
        End If

        If UTIL.CUtil.ConvertText(Me.MyEdit1.Tag) = UTIL.CUtil.ConvertText(Me.txtParentName.Tag) Then
            ShowMessageBox("父类型不能为自己")
            Return False
        End If

        Return True
        DoUpdateCheck = True
    End Function

    Public Overrides Function DoUpdateExist() As Boolean
        DoUpdateExist = False
        MyBase.DoUpdateExist()
        If CStr(Me.参数列表("ID")).Trim = "" Then
            ShowMessageBox("没有选择任何记录")
            Exit Function
        End If
        Dim sb As New StringBuilder

        sb.Append("select * from XscBugType where id='" + CStr(Me.参数列表("ID")) + "'")
        If Not (ct.GetExistSql(sb.ToString())) Then
            ShowMessageBox("此条记录不存在")
            Return False
        End If

        sb = New StringBuilder
        sb.Append(" Select * from XscBugType ")
        sb.Append(" Where Name=? And Type=? ")
        If UTIL.CUtil.ConvertText(Me.txtParentName.Tag) = "" Then
            sb.Append(" And ParentID is Null ")
        Else
            sb.Append(" And ParentID='" + UTIL.CUtil.ConvertText(Me.txtParentName.Tag.TrimEnd()) + "'")
        End If
        Dim cmd As New ADODB.Command

        Dim pa As ADODB.Parameter = cmd.CreateParameter("Name", ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput, 50, Me.MyEdit1.Text.TrimEnd())
        cmd.Parameters.Append(pa)

        pa = cmd.CreateParameter("Type", ADODB.DataTypeEnum.adVarNumeric, ADODB.ParameterDirectionEnum.adParamInput)
        pa.NumericScale = 10
        pa.Precision = 0
        pa.Value = TypeId
        cmd.Parameters.Append(pa)

        cmd.CommandText = sb.ToString()

        Dim rs As ADODB.Recordset = ct.ExcCommandAsRs(cmd)

        If rs Is Nothing Then
            Return True
        Else
            If rs.EOF Then Return True
            If UTIL.CUtil.ConvertText(rs.Fields("ID").Value) = UTIL.CUtil.ConvertText(Me.MyEdit1.Tag) Then
                rs.Close()
                System.Runtime.InteropServices.Marshal.ReleaseComObject(rs) '20100531
                Return True
            Else
                ShowMessageBox("类型名称已经存在")
                MyButton1.Focus()
                Return False
            End If
        End If
        Return True
        DoUpdateExist = True
    End Function

    Public Overrides Sub InitUpdate()
        MyBase.InitUpdate()
        Me.Text = "类型更新"

        Dim sb As New StringBuilder
        sb.Append(" select A.ID,A.Name,A.ParentID,B.NAME AS ParentName,A.Memo ")
        sb.Append(" from XscBugType A ")
        sb.Append(" left join XscBugType B ")
        sb.Append(" On        A.ParentID=B.ID ")
        sb.Append(" Where A.ID='" + CStr(Me.参数列表("ID")) + "'")
        Dim rs As ADODB.Recordset = ct.GetTable(sb.ToString())
        If rs IsNot Nothing Then
            If Not rs.EOF Then

                Me.txtParentName.Text = UTIL.CUtil.ConvertText(rs.Fields("ParentName").Value)
                Me.txtParentName.Tag = UTIL.CUtil.ConvertText(rs.Fields("ParentID").Value)
                Me.MyEdit1.Text = UTIL.CUtil.ConvertText(rs.Fields("Name").Value)
                Me.MyEdit1.Tag = UTIL.CUtil.ConvertText(rs.Fields("ID").Value)
                Me.txtMemo.Text = UTIL.CUtil.ConvertText(rs.Fields("Memo").Value)
            End If
            rs.Close()
            System.Runtime.InteropServices.Marshal.ReleaseComObject(rs) '20100531
            rs = Nothing
        End If
    End Sub

#End Region

End Class
